package com.zyw.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
	public static Boolean valiPathIsExists(String path) {
		return Boolean.valueOf(new File(path).exists());
	}

	public static Boolean valiPathIsFolder(String path) {
		if ((new File(path).exists()) && (new File(path).isDirectory())) {
			return Boolean.valueOf(true);
		}
		return Boolean.valueOf(false);
	}

	public static String formatDirectory(String dir) {
		if (!dir.endsWith(File.separator)) {
			dir = dir + File.separator;
		}
		File dirFile = new File(dir);
		if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
			dirFile = null;
			return null;
		}
		dirFile = null;
		return dir;
	}

	public static void createFolder(String path) {
		new File(path).mkdir();
	}

	public static Boolean deleteFile(String filePath) {
		File file = new File(filePath);
		if ((file.exists()) && (file.isFile())) {
			if (file.delete()) {
				return Boolean.valueOf(true);
			}
			return Boolean.valueOf(false);
		}
		return Boolean.valueOf(false);
	}

	public static List<String> readInDirectoryFileName(String dirPath) {
		if (!dirPath.endsWith(File.separator)) {
			dirPath = dirPath + File.separator;
		}
		File dirFile = new File(dirPath);
		if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
			return null;
		}
		File[] files = dirFile.listFiles();
		List<String> r = new ArrayList<>();
		for (int i = files.length - 1; i >= 0; i--) {
			if (files[i].isFile()) {
				r.add(files[i].getName());
			}
		}
		return r;
	}

	public static List<String> readInDirectoryFileName(String dirPath,
			String suffix) {
		if (!dirPath.endsWith(File.separator)) {
			dirPath = dirPath + File.separator;
		}
		File dirFile = new File(dirPath);
		if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
			return null;
		}
		File[] files = dirFile.listFiles();
		List<String> r = new ArrayList<>();
		for (int i = files.length - 1; i >= 0; i--) {
			if ((files[i].isFile())
					&& (valiFileSuffix(files[i].getName(), suffix)
							.booleanValue())) {
				r.add(files[i].getName());
			}
		}
		return r;
	}

	public static boolean deleteDirectory(String dir) {
		if (!dir.endsWith(File.separator)) {
			dir = dir + File.separator;
		}
		File dirFile = new File(dir);
		if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
			return false;
		}
		boolean flag = true;

		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				flag = deleteFile(files[i].getAbsolutePath()).booleanValue();
				if (!flag) {
					break;
				}
			} else if (files[i].isDirectory()) {
				flag = deleteDirectory(files[i].getAbsolutePath());
				if (!flag) {
					break;
				}
			}
		}
		if (!flag) {
			return false;
		}
		if (dirFile.delete()) {
			return true;
		}
		return false;
	}

	public static boolean deleteFileInDirectoryForSuffix(String dir,
			String suffix) {
		if (!dir.endsWith(File.separator)) {
			dir = dir + File.separator;
		}
		File dirFile = new File(dir);
		if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
			return false;
		}
		boolean flag = true;

		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			if ((files[i].isFile())
					&& (valiFileSuffix(files[i].getName(), suffix)
							.booleanValue())) {
				flag = deleteFile(files[i].getAbsolutePath()).booleanValue();
				if (!flag) {
					break;
				}
			} else if (files[i].isDirectory()) {
				flag = deleteDirectory(files[i].getAbsolutePath());
				if (!flag) {
					break;
				}
			}
		}
		if (!flag) {
			return false;
		}
		if (dirFile.delete()) {
			return true;
		}
		return false;
	}

	public static Boolean valiFileSuffix(String fileName, String suffix) {
		return Boolean
				.valueOf(fileName.split("\\.")[(fileName.split("\\.").length - 1)]
						.equals(suffix));
	}

	public static String getFileSuffix(String fileName) {
		return fileName.split("\\.")[(fileName.split("\\.").length - 1)];
	}

	public static String getFileParentDirectory(File f) {
		String dir = f.getParent();
		if (!dir.endsWith(File.separator)) {
			dir = dir + File.separator;
		}
		return dir;
	}

	public static void writeInLog(String string) {
		File f = new File("D:\\log.txt");
		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
		char[] cbuf = new char[65535];
		String info = "";
		try {
			FileReader fr = new FileReader(f);
			fr.read(cbuf);
			info = String.valueOf(cbuf).trim() + "\r\n";
			fr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		FileWriter fw = null;
		try {
			fw = new FileWriter(f);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			fw.write(info + string);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			fw.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public static String readSimpleFileInfoForText(String fileName,
			String encoding) {
		String lineTxt = null;
		String rText="";
		try {
			File file = new File(fileName);
			InputStreamReader read = null;
			if (encoding == null) {
				read = new InputStreamReader(new FileInputStream(file));
			} else {
				read = new InputStreamReader(new FileInputStream(file),
						encoding);// 考虑到编码格式
			}
			BufferedReader bufferedReader = new BufferedReader(read);
			while ((lineTxt = bufferedReader.readLine()) != null) {
				rText+=lineTxt;
			}
			read.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rText;
	}

	public static String readSimpleFileInfoForText(String fileName) {
		return readSimpleFileInfoForText(fileName, null);
	}
}
